const IA = {
  // 消息提示框  wx-Toast
  Toast: {
    success: (content, image, duration = 2000, mask = false) => {
      wx.showToast({
        title: content,
        icon: "success",
        image: image,
        duration: duration,
        mask: mask,
      });
    },
    fail: (content, image, duration = 2000, mask = false) => {
      wx.showToast({
        title: content,
        icon: "error",
        image: image,
        duration: duration,
        mask: mask,
      });
    },
    alert: (content, image, duration = 2000, mask = false) => {
      wx.showToast({
        title: content,
        icon: "none",
        image: image,
        duration: duration,
        mask: mask,
      });
    },
    hide: () => {
      wx.hideToast();
    },
  },

  // 模态对话框 wx-Modal
  Modal: {
    options: {
      title: "提示",
      content: "",
      showCancel: true,
      cancelText: "取消",
      cancelColor: "#333",
      confirmText: "确定",
      confirmColor: "#ec4141",
      editable: false,
      placeholderText: "请输入内容",
    },
    _show: (options) => {
      if (options.content && options.content.constructor == Object) {
        options.content = JSON.stringify(options.content);
      }
      if (options.content && options.content.indexOf("/") > -1) {
        options.title = options.content.substring(
          0,
          options.content.indexOf("/")
        );
        options.content = options.content.substring(
          options.content.indexOf("/") + 1,
          options.content.length
        );
      }
      let showModalOptions = Object.assign({}, IA.Modal.options, options);
      showModalOptions.confirmText = showModalOptions.confirmText.substring(
        0,
        4
      );
      showModalOptions.cancelText = showModalOptions.cancelText.substring(0, 4);
      return new Promise((resolve, reject) => {
        wx.showModal({
          ...showModalOptions,
          success: (result) => {
            if (result.confirm) {
              return resolve(showModalOptions.content || result.content);
            }
            if (result.cancel) {
              return reject(showModalOptions.content || result.content);
            }
          },
        });
      });
    },
    alert: (content, confirmText = "确定", confirmColor = "#ec4141") => {
      let options = {
        content,
        confirmText,
        confirmColor,
        showCancel: false,
      };
      return IA.Modal._show(options);
    },
    confirm: (content, confirmText = "确定", confirmColor = "#ec4141") => {
      let options = {
        content,
        confirmText,
        confirmColor,
        showCancel: false,
      };
      return IA.Modal._show(options);
    },
    select: (
      content,
      confirmText = "确定",
      cancelText = "取消",
      confirmColor = "#ec4141",
      cancelColor = "#333"
    ) => {
      let options = {
        content,
        confirmText,
        cancelText,
        confirmColor,
        cancelColor,
      };
      return IA.Modal._show(options);
    },
    input: (
      placeholderText = "请输入",
      confirmText = "确定",
      cancelText = "取消",
      confirmColor = "#ec4141",
      cancelColor = "#333"
    ) => {
      let options = {
        confirmText,
        cancelText,
        confirmColor,
        cancelColor,
        editable: true,
        placeholderText,
      };
      return IA.Modal._show(options);
    },
  },
  // 加载提示框 wx-showLoading
  Load: {
    _show: (options) => {
      let showLoadingParams = Object.assign(
        {
          title: "加载中",
          callback: "",
          mask: false,
        },
        options
      );
      return new Promise((resolve, reject) => {
        wx.showLoading({
          ...showLoadingParams,
          success: async (result) => {
            if (showLoadingParams.callback) {
              try {
                let res = await showLoadingParams.callback;
                IA.Load.hide();
                return resolve(res);
              } catch (error) {
                IA.Load.hide();
                return reject(error);
              }
            }
            return resolve(options.title);
          },
        });
      });
    },
    hide: () => {
      wx.hideLoading();
    },
    show: (content = "加载中...", cb) => {
      let options = {
        title: content,
        callback: cb,
      };

      return IA.Load._show(options);
    },
  },
  // 操作菜单
  ActionSheet: {
    name: [],
    callbacks: [],
    params: [],
    _show(options) {
      options.itemList = IA.ActionSheet.name;
      if (options.itemList.length > 6) {
        options.itemList = options.itemList.splice(0, 6);
      }
      let showActionSheetParams = Object.assign({}, options);
      return new Promise((resolve, reject) => {
        wx.showActionSheet({
          ...showActionSheetParams,
          success: async (result) => {
            try {
              let res = await IA.ActionSheet.callbacks[result.tapIndex](
                IA.ActionSheet.params[result.tapIndex]
              );
              return resolve(res);
            } catch (error) {
              return reject(error);
            }
          },
          complete: () => {
            IA.ActionSheet.name = [];
            IA.ActionSheet.callbacks = [];
          },
        });
      });
    },
    show: (items = [], itemColor = "#333", alertText = "提示") => {
      let options = {
        itemList: items || [],
        itemColor,
        alertText,
      };

      if (options.itemList && options.itemList.constructor == String) {
        IA.ActionSheet.name.push(options.itemList);
        IA.ActionSheet.callbacks.push(() => {
          return 0;
        });
        IA.ActionSheet.params.push(options.itemList.params || "");
      }

      if (options.itemList && options.itemList.constructor == Object) {
        options.itemList.name ? "" : (options.itemList.name = "未命名");
        options.itemList.callback
          ? ""
          : (options.itemList.callback = () => {
              return 0;
            });

        IA.ActionSheet.name.push(options.itemList.name);
        IA.ActionSheet.callbacks.push(options.itemList.callback);
        IA.ActionSheet.params.push(options.itemList.params || "");
      }

      if (options.itemList && options.itemList.constructor == Array) {
        options.itemList.length > 0 &&
          options.itemList.map((item, index) => {
            if (item.constructor == String) {
              item = {
                name: item,
                callback: () => {
                  return index;
                },
              };
            }
            if (item.constructor == Object) {
              item.name ? "" : (item.name = "未命名");
              item.callback
                ? ""
                : (item.callback = () => {
                    return index;
                  });
            }

            IA.ActionSheet.name.push(item.name);
            IA.ActionSheet.callbacks.push(item.callback);
            IA.ActionSheet.params.push(item.params || "");
          });
      }

      return IA.ActionSheet._show(options);
    },
  }
};

module.exports = IA;
